package com.ujs.edu.medicine.controller;

import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.YunjiayiErrorMapper;
import com.ujs.edu.medicine.dao.YunjiayiUserMapper;
import com.ujs.edu.medicine.dataobject.Author;
import com.ujs.edu.medicine.dataobject.YunjiayiError;
import com.ujs.edu.medicine.util.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("api/error")
public class ErrorController {
    @Autowired
    private YunjiayiErrorMapper yunjiayiErrorMapper;
    @Autowired
    private YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    private HttpServletRequest request;
    private String gettoken()
        {
            return request.getHeader("Authorization");
        }
//错误上报
    @PostMapping()
    public Result PostError(@RequestBody JSONObject object) {
        String word=object.getString("word");
        int identity=object.getInt("identity");
        List<String> picture=object.getJSONArray("picture");

        Result result = new Result();
        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        } catch (Exception  e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登录");
            return result;
        }
        YunjiayiError error=new YunjiayiError();
        error.setFiles(picture.toString());
        error.setIdentityID(identity);
        error.setUserId(myuserid);
        error.setContent(word);
        int adderror=yunjiayiErrorMapper.insert(error);
        if (adderror == 1) {
            result.setCode(0);
            result.setMessage("添加成功");
        } else {
            result.setCode(1);
            result.setMessage("添加失败");
        }
        return result;
    }
    //获取错误详情
    @GetMapping()
    public Result GetError(@RequestParam(value = "errorID",required = true) int errorID)
    {
        Result result = new Result();
        YunjiayiError error=new YunjiayiError();
        error=yunjiayiErrorMapper.selectByPrimaryKey(errorID);
        if(error!= null)
        {
            result.setCode(0);
            Author author =yunjiayiUserMapper.selectAuthorByPrimaryKey(error.getUserId());
            JSONObject json= new JSONObject();
            json.put("author",author);
            json.put("createTime", error.getCreatetime());
            json.put("content",error.getContent());
            json.put("files",error.getFiles());
            result.setData(json);
            result.setMessage("获取成功");

        }
        else{
            result.setCode(1);
            result.setMessage("获取失败");
        }
       return result;
    }
       
     @GetMapping("/list")
    public Result GetErrorList(@RequestParam(value = "identityID",required = true) int identityID,
                               @RequestParam(value = "page",required = true) int page
    )
    {
        Result result = new Result();
        List<YunjiayiError> errors=new LinkedList<YunjiayiError>();
        if(identityID==-1) {
            errors=yunjiayiErrorMapper.selectALL();
        }
        else{
            errors=yunjiayiErrorMapper.selectByIdentity(identityID);
        }
        if(errors.size()!=0) {
            Integer start = (page - 1) * 10;
            Integer end = page*10<errors.size()?page*10:errors.size();
            JSONObject[] JSONObjectArry = new JSONObject[end%10==0?10:end%10];
            JSONObject json= new JSONObject();
            for (int  j=start,i=0 ; j < end; j++,i++) {
                int userid = errors.get(i).getUserId();
                Author author =yunjiayiUserMapper.selectAuthorByPrimaryKey(userid);
                JSONObjectArry[i] = new JSONObject();
                JSONObjectArry[i].put("id", errors.get(j).getErrorID());
                JSONObjectArry[i].put("author", author);
                JSONObjectArry[i].put("abstract", errors.get(j).getContent());
                JSONObjectArry[i].put("creatime", errors.get(j).getCreatetime());
            }
            result.setCode(0);
            result.setMessage("获取成功");
            json.put("page",page);
            json.put("list",JSONObjectArry);
            result.setData(json);
        }
        else{
            result.setCode( 1);
            result.setMessage("获取失败：无数据记录");

        }
        return result;
    }
}
